package com.hnluchuan.staff.web.controller.admin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.hnluchuan.staff.dto.EmployeeDTO;
import com.hnluchuan.staff.dto.UserDTO;

import com.hnluchuan.staff.model.Education;
import com.hnluchuan.staff.model.User;
import com.hnluchuan.staff.service.EmployeeService;
import com.hnluchuan.utils.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnluchuan.staff.dto.EducationDTO;
import com.hnluchuan.staff.service.EducationService;

import com.hnluchuan.core.support.Page;
import com.hnluchuan.staff.web.controller.BaseController;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/admin/education", produces="text/html;charset=UTF-8")
public class AdminEducationController extends BaseController {

	@Autowired
	private EducationService educationService;
	@Autowired
	private EmployeeService employeeService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id) {
		UserDTO userDTO=getCurrentUserDTO();
		Integer type=userDTO.getType();
		map.put("type", type);
		if (id != null) {
			EducationDTO dto = educationService.load(id);
			map.put("n", dto);
		}
		return "admin/education/education_detail";
	}

	@ResponseBody
	@RequestMapping(value = "/save")
	public String save(EducationDTO dto,String  iDCard) throws Exception {
		UserDTO userDTO = getCurrentUserDTO();
		if (dto.getId() == null) {
			EmployeeDTO employeeDTO=new EmployeeDTO();
			if(userDTO.getType()==1)
			{
		       List<EmployeeDTO> list=employeeService.findAll();
				for (EmployeeDTO e:list
					 ) {
					  if(e.getIDCard().equals(iDCard)){
					  	employeeDTO.setId(e.getId());
					  	break;}
				}
				if(employeeDTO.getId()==null)
				{
					throw new RuntimeException("您还没有添加员工信息！！");
				}
				dto.setEmployee(employeeDTO);
				educationService.create(dto);
			}else {
				 employeeDTO.setUser(userDTO);
				employeeDTO = employeeService.findOne(employeeDTO);
				if(employeeDTO==null)
				{
					throw new RuntimeException("您还没有添加员工信息！！");
				}
				dto.setEmployee(employeeDTO);
				educationService.create(dto);
			}
		} else if(userDTO.getType()==1){
//			throw new RuntimeException("请实现更新逻辑，并删除该行");

			EducationDTO old = educationService.load(dto.getId());
			// 确定哪些字段是在详情页面需要修改的，不在详情页修改的，就不需要在这里set值。updated和updatedBy不需要在这里处理，框架会统一处理
			old.setId(dto.getId());
			old.setEmployee(dto.getEmployee());
			old.setStart(dto.getStart());
			old.setEnd(dto.getEnd());
			old.setMajor(dto.getMajor());
			old.setRecord(dto.getRecord());
			old.setCertificate(dto.getCertificate());
			educationService.updateAllFields(old);

		}else
		{
			throw new RuntimeException("管理员才可以修改！！！");
		}
		return ok();
	}

	@ResponseBody
	@RequestMapping(value = "/delete")
	public String delete(Long[] ids) throws IOException {
		UserDTO dto=getCurrentUserDTO();
		if(dto.getType()!=1)
		{
			throw  new BizException("您没有权限！！");
		}
		educationService.deleteByIds(ids);
		return ok();
	}

	@RequestMapping(value = "/list")
	public String list(EducationDTO dto, Boolean search, Map<String, Object> map, Page page) {
		List<EducationDTO> dtos = educationService.find(dto, page);

		UserDTO userDTO=getCurrentUserDTO();
		EmployeeDTO employeeDTO=new EmployeeDTO();
		employeeDTO.setUser(userDTO);
		employeeDTO=employeeService.findOne(employeeDTO);
		//判断类型如果不是为管理员，默认只显示自己信息
		if(employeeDTO==null&&userDTO.getType()!=1)
		{
			dtos=null;
		}else if(userDTO.getType()!=1)
		{
			EducationDTO e;
			if(dtos.size()>0)
			{
				for(int i=0;i<dtos.size();i++)
				{
					e=dtos.get(i);
					if(e.getEmployee().getId()==employeeDTO.getId())
					{
						continue;
					}else
					{
						dtos.remove(i);
						i--;
					}
				}
			}
		}
		map.put("type", userDTO.getType());
		map.put("list", dtos);
		map.put("query", dto);
		map.put("search", search);
		return "admin/education/education_list";
	}

	@RequestMapping(value = "/search")
	public String search(EducationDTO dto, Map<String, Object> map, Page page) {
		List<EducationDTO> dtos = educationService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		return "admin/education/education_search";
	}


}
